"""Support classes and functions for testing in older versions of Python."""
import unittest as _unittest
import re as _re


__all__ = ['_TestCase']


class _TestCase(_unittest.TestCase):
    # The following method was adapted from unittest2 source
    # code <http://pypi.python.org/pypi/unittest2>.
    # Copyright 2010 Michael Foord, released under the BSD License.
    def assertRaisesRegex(self, expected_exception, expected_regexp,
                           callable_obj=None, *args, **kwargs):
        if callable_obj is None:
            return unittest._AssertRaisesContext(expected_exception, self, expected_regexp)
        try:
            callable_obj(*args, **kwargs)
        except expected_exception, exc_value:
            if isinstance(expected_regexp, str):
                expected_regexp = _re.compile(expected_regexp)
            if not expected_regexp.search(str(exc_value)):
                raise self.failureException('"%s" does not match "%s"' %
                         (expected_regexp.pattern, str(exc_value)))
        else:
            if hasattr(expected_exception, '__name__'):
                excName = expected_exception.__name__
            else:
                excName = str(expected_exception)
            raise self.failureException("%s not raised" % excName)
